﻿using System;
using System.Web.UI;
using TierApplication;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;

public partial class ForeProgram_shopOrderView : System.Web.UI.Page
{

    #region Page_Load
    /// <summary>
    /// Page_Load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblOrderID.Text = Context.Items["OrderId"].ToString();
            LoadUser();
            LoadOrder();
            TotalOrder();
        }
    }
    #endregion

    #region 加载用户信息
    /// <summary>
    /// 加载用户信息
    /// </summary>
    private void LoadUser()
    {
        T_ACCOUNT account = new T_ACCOUNT();
        DataSet ds = account.GetAccount(Security.GetAccountId());
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count != 0)
        {
            lblName.Text = ds.Tables[0].Rows[0]["ACCOUNTNAME"].ToString();
            lblEMail.Text = ds.Tables[0].Rows[0]["EMAIL"].ToString();
            lblMobile.Text = ds.Tables[0].Rows[0]["MOBILE"].ToString();
            lblSchoolName.Text = ds.Tables[0].Rows[0]["SchoolName"].ToString();
        }
    }
    #endregion

    #region 加载数据
    /// <summary>
    /// 加载数据
    /// </summary>
    private void LoadOrder()
    {
        T_ORDER order = new T_ORDER();
        StringBuilder sb = new StringBuilder();
        sb.Append(" And orderid='" + lblOrderID.Text + "'");
        DataSet ds = order.GetOrderForAccount(sb.ToString());
        if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
        {
            lblOrderDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["OrderDate"]).ToString("yyyy-MM-dd HH:mm");
            lblStatusName.Text = ds.Tables[0].Rows[0]["StatusName"].ToString();
            if (ds.Tables[0].Rows[0]["orderstatus"].ToString().Equals("0"))
            {
                imgBank.Visible = true;
                imgCancel.Visible = true;
            }
            else
            {
                imgBank.Visible = false;
                imgCancel.Visible = false;
            }
        }

        T_ORDERDETAIL detail = new T_ORDERDETAIL();
        string strWhere = " And orderid='" + lblOrderID.Text + "'";
        gvTable.DataSource = detail.GetOrderByWhere(strWhere).Tables[0];
        gvTable.DataBind();
    }
    #endregion

    #region 事件
    /// <summary>
    /// 事件 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgBank_Click(object sender, ImageClickEventArgs e)
    {

    }
    
    protected void imgCancel_Click(object sender, ImageClickEventArgs e)
    {
        T_ORDER order = new T_ORDER();
        int ai_result = -1;
        string ac_orderid = string.Empty;
        string ac_result = string.Empty;
        try
        {
            order.CancelOrder(lblOrderID.Text, ref ai_result, ref ac_result);
            if (ai_result == 0)
            {
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('取消交易成功！');</script>");
                Server.Transfer("shopOrderCenter.aspx");
            }
            else
            {
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('" + ac_result + "！');swithBtn('display');</script>");
            }
        }
        catch (Exception ex)
        {
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), DateTime.Now.ToLongTimeString(), "<script>alert('取消交易失败！');</script>");
        }
    }
    

    protected void gvTable_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.DataItemIndex == -1) return;
        HyperLink hlProduct = (HyperLink)e.Row.FindControl("hlProduct");
        hlProduct.NavigateUrl = "ProductView.aspx?productid=" + DataBinder.Eval(e.Row.DataItem, "productid").ToString();
        hlProduct.Text = DataBinder.Eval(e.Row.DataItem, "Productname").ToString();
        e.Row.Cells[6].Text="￥" +(Convert.ToInt32(e.Row.Cells[3].Text)* Convert.ToDecimal(e.Row.Cells[4].Text)).ToString();
    }
    #endregion

    #region 统计购物车
    /// <summary>
    /// 统计购物车
    /// </summary>
    private void TotalOrder()
    {
        Clear();
        if (gvTable != null)
        {
            decimal amount = 0;
            for (int i = 0; i < gvTable.Rows.Count; i++)
            {

                string price = gvTable.Rows[i].Cells[4].Text;
                string quantity = gvTable.Rows[i].Cells[3].Text;
                amount += Convert.ToInt32(quantity) * Convert.ToDecimal(price);
            }
            lbltotalAmount.Text = "￥" + amount.ToString();
        }
    }
    #endregion

    #region 清空
    /// <summary>
    /// 清空
    /// </summary>
    private void Clear()
    {
        lbltotalAmount.Text = "￥0.00";
    }
    #endregion
}